<?php
//分页类
import("ORG.Util.Page");
//公用URL
import("@.ORG.Url");

class ArticleAction extends Action {

    public static $CLASS_ARTICLE = 3;

    //班级文章
    public function index(){
        //left加载
        $result = commonModel::getLeaveData();

        $articl = new Model("article");
        $count = $articl->count();// 总共的条数

        $Page = new Page($count, PAGE_NUMBER);
        $show = $Page->show();// 分页显示输出

        $swenzhang = $articl->select();
        $wenzhang = $articl->field("ar_date,ar_title,ar_content,ar_type,u_id,ar_id")->limit($Page->firstRow.','.$Page->listRows)->order("ar_id desc")->select();

        // 验证添加权限
        $isHasInsert = UserModel::isRoles(UserModel::USER_INSERT);

        // 验证删除权限
        $isHasDelete = UserModel::isRoles(UserModel::USER_DELETE);

        $this->assign("isHasDelete", $isHasDelete);
        $this->assign("isHasInsert", $isHasInsert);

        $this->assign("warray",$wenzhang);
        $this->assign("page",$show);
        $this->assign("result",$result);
        $this->display();
    }
    //班级文章内容页面
     public function Acontent(){
        //left加载
        $result = commonModel::getLeaveData();
        $this->assign("result",$result);
        //查询文章和文章内容
        $articl = new Model("article"); //文章表
        $replay = new Model("replay_zb"); //留言表

        $wid = $this->_get("wid");
        $acontent = $articl->join(" as ar join tp_user us on ar.u_id = us.u_id")->field("ar.ar_content,ar.ar_date,ar.ar_title,ar.ar_id,us.u_name")->where("ar.ar_id='$wid'")->limit(1)->select();

        if (empty($acontent))
        {
            $this->error("没有找到该文章.");
        }

        //文章留言的
        $count = $replay->table("tp_user u,tp_replay_zb r")->where("r.zb_type=3 and r.Link_id=$wid and u.u_id=r.u_id")->count();// 总共的条数
        $Page = new Page($count, PAGE_NUMBER);
        $show = $Page->show();// 分页显示输出
        $wmessage = $replay->table("tp_user u,tp_replay_zb r")->where("r.zb_type=3 and r.Link_id=$wid and u.u_id=r.u_id")->limit($Page->firstRow.','.$Page->listRows)->order("r.zb_id desc")->select();

        //上一个
        $lastRecord = ArticleModel::lastSectionRecord($this->_get('wid'));
        //下一个
        $nextRecord = ArticleModel::nextSectionRecord($this->_get('wid'));

        // 验证删除权限
        $isHasReply = UserModel::isRoles(UserModel::USER_REPLY);
        $this->assign("isHasReply", $isHasReply);

        $this->assign("lastRecord", $lastRecord);
        $this->assign("nextRecord", $nextRecord);

        $this->assign("acontent",$acontent[0]);
        $this->assign("wmessage",$wmessage);
        $this->assign("page",$show);
        $this->assign("count",$count);
        $this->display();
    }
    //发表评论
    public function addmessage()
    {
        // 验证删除权限
        $isHasReply = UserModel::isRoles(UserModel::USER_REPLY);

        if (empty($isHasReply)) {
            $this->error("您没有回复权限，如有问题，请咨询管理员！");
        }

        $replay = new Model("replay_zb"); //留言表
        $data = array(
            'zb_content' => $this->_post('mcontent'),
            'zb_date' =>  date("Y-m-d H-i-s"),
            'zb_type' => self::$CLASS_ARTICLE,
            'u_id' => session('u_id'),
            'Link_id' => $this->_post("aid"),
            );

        $add = $replay->add($data);

        if ($add)
        {
            $this->success();
        }
        else
        {
            $this->error();
        }
    }
    //添加文章页面
    public function Acontent_add(){
        $this->display();
    }

    public  function Acontent_addwen(){
        // 验证添加权限
        $isHasInsert = UserModel::isRoles(UserModel::USER_INSERT);

        if (empty($isHasInsert)) {
            $this->error('您没有添加权限，如有问题，请咨询管理员！');
        }

        $articl = new Model("article"); //文章表
        //准备数据.
        $data = array(
            'ar_title' => $this->_post('ar_title'),
            'ar_content' => $this->_post('ar_content'),
            'ar_date' => time(),
            'ar_type' => 1,
            'u_id' => session('u_id'),
        );

        //加载自动验证.
        if ($articl->create($data))
        {
            if ($articl->add($data))
            {
                $this->success("添加成功！", __URL__);
            }
            else
            {
                $this->error("添加失败！");
            }
        }
        else
        {
            $this->error("添加失败！");
        }
    }

    //删除操作.
    public function AcontentDel(){
        // 验证删除权限
        $isHasDelete = UserModel::isRoles(UserModel::USER_DELETE);

        if (empty($isHasDelete)) {
            $this->error("您没有删除权限，如有问题，请咨询管理员！");
        }

        if ($this->_get('did'))
        {
            $article = M("article");
            //事务开启
            $article->startTrans();

            //删除留言
            $arr = $article->where("ar_id=".$this->_get('did'))->delete();

            //删除留言回复
            M("replay_zb")->where("Link_id=" . $this->_get('mid') . " and zb_type=" . self::$CLASS_ARTICLE)->delete();

            if ($arr)
            {
                //事物提交.
                $success = $leave->commit();

                if ($success)
                {
                     $this->error("删除失败！");
                }
                else
                {
                    $this->success("删除成功！");
                }
            }
            else
            {
                $this->error("删除失败！");
            }
        }
        else
        {
            $this->error("删除失败！");
        }
    }
}
?>
